package com.blog.common.domain.util;

import com.blog.common.core.context.ClassScanner;
import com.blog.common.core.exception.FileAbsentException;
import com.blog.common.domain.annotation.Convert;
import com.blog.common.domain.strategy.ConvertStrategy;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 自定义字段解析类
 * </p>
 *
 * @author Valerie
 * @date 2023/5/28
 **/
@Slf4j
public class FileUtils {


    /**
     * 递归向类中设置注解标记字段
     *
     * @param entity
     * @return void
     */
    @SneakyThrows
    public static void assignImageFields(Object entity) {
        if (entity instanceof List) {
            List<Object> list = (List<Object>) entity;
            for (Object item : list) {
                assignImageFields(item);
            }
        } else {
            Class<?> clazz = entity.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);

                // 判断字段是否为列表类型
                if (List.class.isAssignableFrom(field.getType())) {

                    List<Object> nestedList = (List<Object>) field.get(entity);
                    if (nestedList != null) {
                        for (Object nestedItem : nestedList) {
                            // 递归处理嵌套列表中的每个元素
                            assignImageFields(nestedItem);
                        }
                    }
                } else {
                    // 判断字段是否为实体类类型
                    if (ClassScanner.isEntity(field.getType())) {

                        Object nestedEntity = field.get(entity);
                        if (nestedEntity != null) {
                            // 递归处理嵌套的实体类
                            assignImageFields(nestedEntity);
                        }

                    }
                }

                // 判断字段是否为图片字段
                if (field.isAnnotationPresent(Convert.class)) {

                    if (List.class.isAssignableFrom(field.getType())) {
                        List<String> pathList = (List<String>) field.get(entity);
                        pathList = pathList.stream()
                                .map(p -> ConvertStrategy.covertByStrategy(
                                        field.getAnnotation(Convert.class), p))
                                .collect(Collectors.toList());
                        field.set(entity, pathList);
                    } else {
                        // 在这里为图片字段赋值
                        String value = Optional.ofNullable(field.get(entity)).orElse("").toString();
                        String path = ConvertStrategy.covertByStrategy(field.getAnnotation(Convert.class), value);
                        field.set(entity, path);
                    }
                }
            }
        }
    }
}
